﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Shapes;

namespace ThunderboltCustomControlLibrary;

public class WithArrowToolTip:ToolTip
{
	static WithArrowToolTip()
	{
		DefaultStyleKeyProperty.OverrideMetadata(typeof(WithArrowToolTip), new FrameworkPropertyMetadata(typeof(WithArrowToolTip)));
	}

	/// <summary>
	///气泡位置
	/// </summary>       
	public BubblePosition TipPosition
	{
		get { return (BubblePosition)GetValue(TipPositionProperty); }
		set { SetValue(TipPositionProperty, value); }
	}
	public static readonly DependencyProperty TipPositionProperty =
		DependencyProperty.Register("TipPosition", typeof(BubblePosition), typeof(WithArrowToolTip), new PropertyMetadata(BubblePosition.Top));


	/// <summary>
	///提示文本
	/// </summary>       
	[Description("Gets or sets  prompt text")]
	[Category("Common Properties")]
	public string ToolTipText
	{
		get { return (string)GetValue(ToolTipTextProperty); }
		set { SetValue(ToolTipTextProperty, value); }
	}
	public static readonly DependencyProperty ToolTipTextProperty =
		DependencyProperty.Register("ToolTipText", typeof(string), typeof(WithArrowToolTip), new PropertyMetadata(string.Empty));


	public override void OnApplyTemplate()
	{
		base.OnApplyTemplate();

	}

	protected override void OnOpened(RoutedEventArgs e)
	{
		var p = this.Placement;
		var borderPath = this.Template.FindName("PART_path", this) as Path;
		//var borderGrid = this.Template.FindName("PART_grid", this) as Border;

		borderPath.Data = GetPathGeometry(this.ActualWidth-15, this.Height-15, 5);


	}

	private  PathGeometry GetPathGeometry(double width, double height, double r)
	{
		//width += 2 * r;
		//height += 2 * r;
		PathGeometry geometry = new PathGeometry();
		PathFigure pathfigure = new PathFigure();
		pathfigure.IsClosed = true;
		switch (this.Placement)
		{
			case PlacementMode.Top:
				pathfigure.StartPoint = new Point(r, 0);
				pathfigure.Segments.Add(new ArcSegment(new Point(0, r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(0, height + r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(r, height + 2 * r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(width / 2, height + 2 * r), true));
				pathfigure.Segments.Add(new LineSegment(new Point(width / 2 + r, height + 3 * r), true));
				pathfigure.Segments.Add(new LineSegment(new Point(width / 2 + 2 * r, height + 2 * r), true));
				pathfigure.Segments.Add(new LineSegment(new Point(width + r, height + 2 * r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(width + 2 * r, height + r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(width + 2 * r, r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(width + r, 0), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				break;
			case PlacementMode.Bottom:
				pathfigure.StartPoint = new Point(r, r);
				pathfigure.Segments.Add(new ArcSegment(new Point(0, 2*r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(0, height +2* r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(r, height + 3 * r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
			pathfigure.Segments.Add(new LineSegment(new Point(width + r, height + 3 * r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(width +  2*r, height +2* r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(width + 2 * r, 2*r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(width + r, r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				
				pathfigure.Segments.Add(new LineSegment(new Point(width / 2+2*r, r), true));
				pathfigure.Segments.Add(new LineSegment(new Point(width / 2 + r, 0), true));
				pathfigure.Segments.Add(new LineSegment(new Point(width / 2 ,  r), true));

				break;
			case PlacementMode.Left:
				pathfigure.StartPoint = new Point(r, 0);
				pathfigure.Segments.Add(new ArcSegment(new Point(0, r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(0, height + r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(r, height + 2 * r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(width + r, height + 2 * r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(width + 2 * r, height +  r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(width +2* r, height/2+2 * r), true));
				pathfigure.Segments.Add(new LineSegment(new Point(width +3* r, height/2+r), true));
				pathfigure.Segments.Add(new LineSegment(new Point(width  + 2 * r, height/2), true));
				pathfigure.Segments.Add(new LineSegment(new Point(width  +2* r, r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(width +r, 0), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));

				break;
			case PlacementMode.Right:
				pathfigure.StartPoint = new Point(r*2, 0);
				pathfigure.Segments.Add(new ArcSegment(new Point(r, r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(r, height /2), true));
				pathfigure.Segments.Add(new LineSegment(new Point(0, height/2 +  r),  true));
				pathfigure.Segments.Add(new LineSegment(new Point(r, height/2 + 2 * r), true));
				pathfigure.Segments.Add(new LineSegment(new Point(r, height + r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(2 * r, height  + 2 * r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(width+r*2, height   +2* r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(width + 3 * r, height +r), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));
				pathfigure.Segments.Add(new LineSegment(new Point(width + 3 * r, r), true));
				pathfigure.Segments.Add(new ArcSegment(new Point(width + 2*r, 0), new Size(r, r), 0, false, SweepDirection.Counterclockwise, true));

				break;
		}
		
		
		geometry.Figures.Add(pathfigure);
		return geometry;
	}
}

public enum BubblePosition
{
	Top, Bottom, Left , Right
}